<template>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="60%" :before-close="handleClose">
        <el-form :model="form" class="viewOrExamine" ref="form" label-width="110px">
            <el-form-item label="保单号">
                <el-input v-model="form.insureNum" placeholder="请输入保单号"></el-input>
            </el-form-item>
            <el-form-item label="上传保单">
                <el-upload :headers="headers" :auto-upload="true" action="/file/upload" :http-request="uploadFile"
                    :before-upload="beforeUpload" :on-success="onSuccess" :show-file-list="false">
                    <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
                <el-col :span="24" v-if="!!insureUrl">
                    <div class="block">
                        <a style="width: 100%; height: 600px" :href="insureUrl" download="">{{
        insureUrl.substring(insureUrl.lastIndexOf('/') + 1) }}</a>
                    </div>
                </el-col>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="returnPolicy">确定</el-button>
            <el-button @click="close">关闭</el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            headers: { "Content-Type": "multipart/form-data" },
            insureUrl: '',
            title: "回传保单",
            dialogFormVisible: false,
            form: {
                insureNum: ''
            },
            row: {},
            file: null,

        };
    },
    methods: {
        close() {
            this.handleClose()
        },
        handleClose(done) {
            console.log(this.row.insureUrl, this.insureUrl, 'fffff')
            if (this.row.insureUrl && this.row.insureUrl !== this.insureUrl) {
            this.$confirm('您还未保存是否确认关闭？')
                .then(_ => {
                    this.dialogFormVisible = false
                    done();
                })
                .catch(_ => { });
            } else {
                this.dialogFormVisible = false
            }
        },
        onSuccess(res, file) {
            if (res.code * 1 === 200) {
                this.info.businessLicense = file;
                this.$message.suucess("上传成功");
            } else {
                this.$message.error("上传失败");
            }
        },
        beforeUpload(file) {
            this.file = file;
            if (
                file.type !== "application/pdf"
            ) {
                this.$message.warning("请上传pdf");
                return false;
            }
            return true;
        },
        uploadFile() {
            const headers = {
                "Content-Type": "multipart/form-data",
            };
            const formData = new FormData();
            formData.append("file", this.file);
            this.$post("/file/upload", formData, headers).then((res) => {
                this.insureUrl = res.data;
            });
        },
        returnPolicy() {
            if (!this.form.insureNum) {
                this.$message.error('请填写保单号')
                return
            }
            const params = {
                warrantyId: this.row.warrantyId,
                insureUrl: this.insureUrl,
                insureNum: this.form.insureNum,
            }
            this.$post(`/warranty/insure/upInfo`, params).then((res) => {
                this.$message.success('回传成功') // 成功提示信息
                this.dialogFormVisible = false
                this.$emit('initTable')
            });
        }
    }
};

</script>
<style scoped lang="scss"></style>